package cn.merryyou.generator.service;

import cn.merryyou.generator.config.GenConfig;
import cn.merryyou.generator.domain.ColumnInfo;
import cn.merryyou.generator.domain.TableInfo;
import cn.merryyou.generator.service.utils.GenInterFfcs;
import cn.merryyou.generator.service.utils.GenInterMybatisPlus;
import cn.merryyou.generator.service.utils.GenInterfaces;
import cn.merryyou.generator.util.Constants;
import cn.merryyou.generator.util.StringUtils;
import org.apache.velocity.VelocityContext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 代码生成器 工具类
 *
 * @author ruoyi
 */
public class GenUtils {
      /**
     * 类型转换
     */
    public static Map<String, String> javaTypeMap = new HashMap<String, String>();

    /**
     * 设置列信息
     */
    public static List<ColumnInfo> transColums(List<ColumnInfo> columns) {
        // 列信息
        List<ColumnInfo> columsList = new ArrayList<>();
        for (ColumnInfo column : columns) {
            // 列名转换成Java属性名
            String attrName = StringUtils.convertToCamelCase(column.getColumnName());
            column.setAttrName(attrName);
            column.setAttrname(StringUtils.uncapitalize(attrName));
            column.setExtra(column.getExtra());

            // 列的数据类型，转换成Java类型
            String attrType = javaTypeMap.get(column.getDataType());
            column.setAttrType(attrType);

            columsList.add(column);
        }
        return columsList;
    }

    /**
     * 获取模板信息
     *
     * @return 模板列表
     */
    public static VelocityContext getVelocityContext(GenInfo genInfo, TableInfo table) {
        GenInterfaces genInterfaces;
        if(genInfo.getStyle().equals("ffcs")){
            genInterfaces = new GenInterFfcs();
            return genInterfaces.getVelocityContext(genInfo,table);
        }
        genInterfaces = new GenInterMybatisPlus();
        return genInterfaces.getVelocityContext(genInfo,table);
    }

    /**
     * 获取模板信息
     *
     * @return 模板列表
     */
    public static List<String> getTemplates(String path) {
        GenInterfaces genInterfaces;
        if(path.equals("ffcs")){
            genInterfaces = new GenInterFfcs();
            return genInterfaces.getTemplates(path);
        }else {
            genInterfaces = new GenInterMybatisPlus();
            return genInterfaces.getTemplates(path);
        }
    }

    /**
     * 消防框架的模板代码
     */
    public static String getFileName(GenInfo genInfo, String template, TableInfo table) {
        GenInterfaces genInterfaces;
        if(genInfo.getStyle().equals("ffcs")){
            genInterfaces = new GenInterFfcs();
            return genInterfaces.getFileName(genInfo,template,table);
        }else {
            genInterfaces = new GenInterMybatisPlus();
            return genInterfaces.getFileName(genInfo,template,table);
        }
    }


    /**
     * 表名转换成Java类名
     */
    public static String tableToJava(String tableName) {
        String autoRemovePre = GenConfig.getAutoRemovePre();
        String tablePrefix = GenConfig.getTablePrefix();
        if (Constants.AUTO_REOMVE_PRE.equals(autoRemovePre) && StringUtils.isNotEmpty(tablePrefix)) {
            tableName = tableName.replaceFirst(tablePrefix, "");
        }
        return StringUtils.convertToCamelCase(tableName);
    }

    /**
     * 获取模块名
     *
     * @param packageName 包名
     * @return 模块名
     */
    public static String getModuleName(String packageName) {
        int lastIndex = packageName.lastIndexOf(".");
        int nameLength = packageName.length();
        String moduleName = StringUtils.substring(packageName, lastIndex + 1, nameLength);
        return moduleName;
    }

    public static String getBasePackage(String packageName) {
        int lastIndex = packageName.lastIndexOf(".");
        String basePackage = StringUtils.substring(packageName, 0, lastIndex);
        return basePackage;
    }

    public static String replaceKeyword(String keyword) {
        String keyName = keyword.replaceAll("(?:表|信息|管理)" , "");
        return keyName;
    }

    static {
        javaTypeMap.put("tinyint" , "Integer");
        javaTypeMap.put("smallint" , "Integer");
        javaTypeMap.put("mediumint" , "Integer");
        javaTypeMap.put("int" , "Integer");
        javaTypeMap.put("number" , "Integer");
        javaTypeMap.put("integer" , "integer");
        javaTypeMap.put("bigint" , "Long");
        javaTypeMap.put("float" , "Float");
        javaTypeMap.put("double" , "Double");
        javaTypeMap.put("decimal" , "BigDecimal");
        javaTypeMap.put("bit" , "Boolean");
        javaTypeMap.put("char" , "String");
        javaTypeMap.put("varchar" , "String");
        javaTypeMap.put("varchar2" , "String");
        javaTypeMap.put("tinytext" , "String");
        javaTypeMap.put("text" , "String");
        javaTypeMap.put("mediumtext" , "String");
        javaTypeMap.put("longtext" , "String");
        javaTypeMap.put("time" , "Date");
        javaTypeMap.put("date" , "Date");
        javaTypeMap.put("datetime" , "Date");
        javaTypeMap.put("timestamp" , "Date");
    }
}
